﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace landlord2
{
    public class TwoStateButton : Button
    {
        public static readonly DependencyProperty IsDownProperty = DependencyProperty.Register("IsDown", typeof(bool), typeof(TwoStateButton),
            new FrameworkPropertyMetadata(false, OnIsDownPropertyChanged));

        public bool IsDown
        {
            get
            {
                return Convert.ToBoolean(GetValue(IsDownProperty));
            }
            set
            {
                SetValue(IsDownProperty, value);
                try
                {
                    RaiseEvent(new DownEventArgs(DownEvent, this, IsDown));
                }
                catch { }
            }
        }

        public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(ImageSource), typeof(TwoStateButton),
            new FrameworkPropertyMetadata(null, OnSourcePropertyChanged));

        public ImageSource Source
        {
            get
            {
                return (ImageSource)GetValue(SourceProperty);
            }
            set
            {
                SetValue(SourceProperty, value);
            }
        }

        public static readonly DependencyProperty SourceWidthProperty = DependencyProperty.Register("SourceWidth", typeof(double), typeof(TwoStateButton),
            new FrameworkPropertyMetadata(16.0, OnSourcePropertyChanged));

        public double SourceWidth
        {
            get
            {
                return (double)GetValue(SourceWidthProperty);
            }
            set
            {
                SetValue(SourceWidthProperty, value);
            }
        }

        public static readonly DependencyProperty SourceHeightProperty = DependencyProperty.Register("SourceHeight", typeof(double), typeof(TwoStateButton),
            new FrameworkPropertyMetadata(16.0, OnSourcePropertyChanged));

        public double SourceHeight
        {
            get
            {
                return (double)GetValue(SourceHeightProperty);
            }
            set
            {
                SetValue(SourceHeightProperty, value);
            }
        }

        public static readonly DependencyProperty DownSourceProperty = DependencyProperty.Register("DownSource", typeof(ImageSource), typeof(TwoStateButton),
            new FrameworkPropertyMetadata(null, OnDownSourcePropertyChanged));

        public ImageSource DownSource
        {
            get
            {
                return (ImageSource)GetValue(DownSourceProperty);
            }
            set
            {
                SetValue(DownSourceProperty, value);
            }
        }

        public static readonly DependencyProperty DownSourceWidthProperty = DependencyProperty.Register("DownSourceWidth", typeof(double), typeof(TwoStateButton),
            new FrameworkPropertyMetadata(16.0, OnSourcePropertyChanged));

        public double DownSourceWidth
        {
            get
            {
                return (double)GetValue(DownSourceWidthProperty);
            }
            set
            {
                SetValue(DownSourceWidthProperty, value);
            }
        }

        public static readonly DependencyProperty DownSourceHeightProperty = DependencyProperty.Register("DownSourceHeight", typeof(double), typeof(TwoStateButton),
            new FrameworkPropertyMetadata(16.0, OnSourcePropertyChanged));

        public double DownSourceHeight
        {
            get
            {
                return (double)GetValue(DownSourceHeightProperty);
            }
            set
            {
                SetValue(DownSourceHeightProperty, value);
            }
        }

        public static readonly RoutedEvent DownEvent = EventManager.RegisterRoutedEvent("Down", RoutingStrategy.Bubble,
            typeof(DownEventHandler), typeof(TwoStateButton));

        public event DownEventHandler Down
        {
            add { AddHandler(DownEvent, value); }
            remove { RemoveHandler(DownEvent, value); }
        }

        protected override void OnClick()
        {
            base.OnClick();
            IsDown = !IsDown;
            RaiseEvent(new DownEventArgs(DownEvent, this, IsDown));
        }

        private static void OnIsDownPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            TwoStateButton btn = (TwoStateButton)obj;
            btn.ShowImage();
        }

        private static void OnSourcePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            TwoStateButton btn = (TwoStateButton)obj;
            btn.ShowImage();
        }

        private static void OnDownSourcePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            TwoStateButton btn = (TwoStateButton)obj;
            btn.ShowImage();
        }

        private void ShowImage()
        {
            Image img = new Image();
            if (IsDown)
            {
                img.Source = DownSource;
                img.Width = DownSourceWidth;
                img.Height = DownSourceHeight;
            }
            else
            {
                img.Source = Source;
                img.Width = SourceWidth;
                img.Height = SourceHeight;
            }
            Content = img;
        }
    }

    public class DownEventArgs : RoutedEventArgs
    {
        private bool m_isDown;

        public DownEventArgs(bool isDown)
            : base()
        {
            m_isDown = isDown;
        }

        public DownEventArgs(RoutedEvent routedEvent, bool isDown)
            : base(routedEvent)
        {
            m_isDown = isDown;
        }

        public DownEventArgs(RoutedEvent routedEvent, object source, bool isDown)
            : base(routedEvent, source)
        {
            m_isDown = isDown;
        }

        public bool IsDown
        {
            get
            {
                return m_isDown;
            }
        }
    }

    public delegate void DownEventHandler(object sender, DownEventArgs e);
}
